package com.qding.park.common.utils.csv;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import com.qding.park.common.utils.string.StringUtil;

/**
 * @ClassName CsvParser
 * @Description TODO(csv 解析器)
 * @author LeiYang
 * @Date 2017年5月17日 上午9:51:30
 * @version 1.0.0
 */
public class CsvParserHelper {

    /**
     * @ClassName ParserIterator
     * @Description TODO(解析迭代器)
     * @author LeiYang
     * @Date 2017年5月17日 上午10:11:14
     * @version 1.0.0
     */
    public static interface ParserIterator {
        
        String[] parseNext();

        void stopParsing();
        
        String getFileName();
        
        String  getFileTimeStr();
    }

    private static final String CHARSET_NAME = "GBK";

    // /**
    // * @Description (TODO获取csv Reader)
    // * @param realfilepath
    // * @return
    // * @throws FileNotFoundException
    // */
    // private static Reader getReader(String realfilepath, String charsetName) throws FileNotFoundException {
    //
    // try {
    // InputStream io = new FileInputStream(realfilepath);
    // return new InputStreamReader(io, charsetName);
    // } catch (UnsupportedEncodingException e) {
    // throw new IllegalStateException("Unable to read input", e);
    // }
    // }

    private static BufferedReader getReader(File realfile, String charsetName) throws FileNotFoundException {

        try {
            InputStream io = new FileInputStream(realfile);

            BufferedReader reader = new BufferedReader(new InputStreamReader(io, charsetName));
            return reader;
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException("Unable to read input", e);
        }
    }

    private static BufferedReader getReader(File realfile) throws FileNotFoundException {
        return getReader(realfile, CHARSET_NAME);
    }

    // /**
    // * @Description (TODO 一次解析所有的行)
    // * @param realfile
    // * csv文件对象
    // * @param lineSeparator
    // * 行分隔符
    // * @param charsetName
    // * 字符编码
    // * @return
    // * @throws FileNotFoundException
    // */
    // public static List<String[]> parseAll(File realfile, String lineSeparator, String charsetName)
    // throws FileNotFoundException {
    // CsvParserSettings settings = new CsvParserSettings();
    // settings.getFormat().setLineSeparator(lineSeparator);
    // CsvParser parser = new CsvParser(settings);
    // List<String[]> allRows = parser.parseAll(getReader(realfile, charsetName));
    // return allRows;
    // }

    // /**
    // * @Description (TODO一次解析所有行)
    // * @param realfile
    // * 文件
    // * @param lineSeparator
    // * 行分隔符
    // * @return
    // * @throws FileNotFoundException
    // */
    // public static List<String[]> parseAll(File realfile, String lineSeparator) throws FileNotFoundException {
    // return parseAll(realfile,lineSeparator,CHARSET_NAME);
    // }

    /**
     * @Description (TODO创建迭代器)
     * @param realfile
     *            真实文件
     * @param lineSeparator
     *            行分隔符
     * @param charsetName
     *            字符编码
     * @return 迭代器
     * @throws IOException
     */
    public static ParserIterator beginParsing(final File realfile, final String lineSeparator, String charsetName)
            throws IOException {

        final BufferedReader reader = getReader(realfile, charsetName);
        String str = null;
        ParserIterator parserIterator = new ParserIterator() {

            @Override
            public String[] parseNext() {

                try {
                    String str = reader.readLine();
                    if (StringUtil.isBlank(str)) {
                        stopParsing();// 关闭文件名柄
                        return null;
                    }
                    return str.split(lineSeparator);
                } catch (Exception e) {
                    e.printStackTrace();
                    stopParsing();

                }
                return null;
                // return parser.parseNext();
            }

            @Override
            public void stopParsing() {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public String getFileName() {
                // TODO Auto-generated method stub
                return realfile.getName();
            }

            @Override
            public String getFileTimeStr() {
                String fileName = realfile.getName();
                String name = fileName.substring(0, fileName.lastIndexOf("."));// 去掉后缀名
                String timestr = name.substring(name.lastIndexOf("_")+1);
                return timestr;
            }
            

        };

        return parserIterator;

        // MyCsvParserSettings settings = new MyCsvParserSettings();
        // settings.getFormat().setLineSeparator(lineSeparator);
        // settings.setLineSeparatorDetectionEnabled(false);
        // final CsvParser parser = new CsvParser(settings);
        // parser.beginParsing(getReader(realfile, charsetName));
        //
        // ParserIterator parserIterator = new ParserIterator() {
        //
        // @Override
        // public String[] parseNext() {
        // return parser.parseNext();
        // }
        //
        // };
        //
        // return parserIterator;
    }

    public static ParserIterator beginParsing(File realfile, String lineSeparator) throws IOException {
        return beginParsing(realfile, lineSeparator, CHARSET_NAME);
    }

}
